﻿set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

If exists(select 1 from sysobjects where xtype = 'P' and name = 'Orden_Get')
Begin
   Drop proc Orden_Get
End

GO

CREATE  Proc [dbo].[Orden_Get]
	(@Emp_Id smallint,
	 @SoloActivos bit) AS
Begin
	Set nocount on

	IF (@SoloActivos = 1)
	Begin
		SELECT	o.Emp_id AS EmpresaId, Orden_Id as Id, o.Cotizacion_Id AS CotizacionId, t.Cliente_Nombre AS Cliente, 
			o.Num_Pedido AS NumeroPedido, Cotizacion_FecAplicada AS FechaAplicada, 
			Fec_Inicio AS FechaInicio, Fec_Final AS FechaFinal, 0 AS NumeroDias
		FROM Orden o
		INNER JOIN Cotizacion c ON (o.Cotizacion_Id = c.Cotizacion_id)
		INNER JOIN Cliente t ON (c.cliente_id = t.cliente_id)
		WHERE	o.Emp_Id = @Emp_Id
		AND		(o.Fec_Final IS NULL OR o.Fec_Final > Getdate())
	End
	ELSE
	Begin
		SELECT	o.Emp_id AS EmpresaId, Orden_Id as Id, o.Cotizacion_Id AS CotizacionId, t.Cliente_Nombre AS Cliente, 
			o.Num_Pedido AS NumeroPedido, Cotizacion_FecAplicada AS FechaAplicada, 
			Fec_Inicio AS FechaInicio, Fec_Final AS FechaFinal, 0 AS NumeroDias
		FROM Orden o
		INNER JOIN Cotizacion c ON (o.Cotizacion_Id = c.Cotizacion_id)
		INNER JOIN Cliente t ON (c.cliente_id = t.cliente_id)
		WHERE	o.Emp_Id = @Emp_Id
	End
	Set nocount on
End

Go

  